package com.palmgo.utils.drawer.basic;

import java.util.List;

import com.palmgo.utils.drawer.model.LatLng;

public class DecodeUtils {
	/**
	 *   google map 折线解码
	 * @param encoded 密文字符串
	 * @param points 坐标列表
	 */
	public static void decodePoly(String encoded, List<LatLng> points) {
		try {
			encoded = encoded.replaceAll("\\\\\\\\", "\\\\");
			// java中，\为特殊字符，会自动为\多加一次变成\\
			// 比如_vcsFsxbyU~ExXzHt\\tqBjzL会变成_vcsFsxbyU~ExXzHt\\\\tqBjzL
			// List<LocationPoint> poly = new ArrayList<LocationPoint>();
			int index = 0, len = encoded.length();
			int lat = 0, lng = 0;
			points.clear();
			while (index < len) {
				int b, shift = 0, result = 0;
				do {
					b = encoded.charAt(index++) - 63;
					result |= (b & 0x1f) << shift;
					shift += 5;
				} while (b >= 0x20);
				int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
				lat += dlat;
				//
				shift = 0;
				result = 0;
				do {
					b = encoded.charAt(index++) - 63;
					result |= (b & 0x1f) << shift;
					shift += 5;
				} while (b >= 0x20);
				int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
				lng += dlng;
				points.add(new LatLng(lat, lng));
			}
		} catch (Exception e) {
		}
		// return poly;
	}
}
